<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 寻找比目标字母大的最小字母 -->
    <script>
      var nextGreatestLetter = function (letters, target) {
        let len = letters.length
        if (target >= letters[len - 1]) return letters[0]
        let left = 0
        let right = len - 1 // 此处使用 len-1，是因为减去上面那种情况
        // 由于不会重复，则使用 [）,左闭右开
        while (left < right) {
          let mid = left + Math.floor((right - left) / 2)
          if (letters[mid] > target) {
            right = mid
          } else {
            // 当前元素小于等于 target，返回下一个元素的索引
            left = mid + 1
          }
        }
        return letters[left]
      }
      console.log(nextGreatestLetter(['c', 'f', 'j'], 'a'))
    </script>
  </body>
</html>
